package com.note.simpleadmin.module.shop.service;

import com.note.simpleadmin.entity.TbColumn;
import com.note.simpleadmin.entity.TbCoupon;
import com.note.simpleadmin.enu.ActiveEnum;
import com.note.simpleadmin.enu.DeleteEnum;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.exception.DataRepeatException;
import com.note.simpleadmin.exception.ParamException;
import com.note.simpleadmin.module.shop.dao.CouponRepository;
import com.note.simpleadmin.utils.IdGenerator;
import com.note.simpleadmin.vo.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/***
 *@ClaseName:CouponService
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/2/28 0028 19:35
 */
@Service
public class CouponService {
    @Autowired
    private CouponRepository couponRepository;
    public TbCoupon getOne(String id) {
        return couponRepository.findByIdAndIsDelete(id,DeleteEnum.NORMAL.getValue());
    }
    public TbCoupon findByCode(String code) {
        return couponRepository.findByCodeAndIsDelete(code,DeleteEnum.NORMAL.getValue());
    }
    public void save(TbCoupon param) throws  ParamException,DataRepeatException{
        if(StringUtils.isBlank(param.getCode())) {
            throw new ParamException("code不为空");
        }
        TbCoupon coupon=findByCode(param.getCode());
        if(coupon!=null) {
            throw new DataRepeatException("code已经存在");
        }
        param.setId(IdGenerator.id());
        param.setIsDelete(DeleteEnum.NORMAL.getValue());
        param.setDrawed(0);
        param.setUsed(0);
        param.setActive(ActiveEnum.FORBBIDEN.getValue());
        couponRepository.save(param);
    }
    public PageBean findByPage(String name, Integer pageNumber)
    {
        if (pageNumber == null) {
            pageNumber = 0;
        }
        else
        {
            pageNumber=pageNumber-1;
        }
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        PageRequest pageRequest = new PageRequest(pageNumber, 20, sort);
        Specification<TbCoupon> specification=new Specification<TbCoupon>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Predicate toPredicate(Root<TbCoupon> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(name)) {
                    predicates.add(criteriaBuilder.equal(root.get("name"), name));
                }
                return  criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page<TbCoupon> page = couponRepository.findAll(specification,pageRequest);
        List<TbCoupon> environments=page.getContent();
        PageBean<TbCoupon> pageBean=new PageBean(page.getTotalPages(),page.getTotalElements(),page.isFirst(),page.isEmpty(),page.getNumber(), environments);;
        return pageBean;
    }
    public void forbidCoupon(String id,Integer active) throws  DataNotExistException{
        TbCoupon tbCoupon=getOne(id);
        if(tbCoupon==null)
        {
            throw new DataNotExistException("coupon not exist");
        }
        tbCoupon.setActive(active);
        couponRepository.save(tbCoupon);
    }
}
